﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using Microsoft.Win32;
using SDD.Properties;
using Google.GData.Documents;
using Google.GData.Client;
using Google.GData.Client.ResumableUpload;

namespace SDD
{
    class UpdateData
    {
        //A child form for the user to interact with.
        //private OptionsForm optionsForm;
        //Keeps track of our logged in state.
        public bool loggedIn = false;
        //A connection with the DocList API.
        private DocumentsService service;
        //The name of the shell context menu option.
        private const string KEY_NAME = "Send to Google Docs";
        //The timer in milliseconds to display balloon tips.
        private const int BALLOON_TIMER = 10000;
        //The most recently uploaded document.
        private DocumentEntry lastUploadEntry = null;
        
        public void Login(string username, string password)
        {
            if (loggedIn)
            {
                //throw new ApplicationException("Already logged in.");
            }
            try
            {
                service = new DocumentsService("DocListUploader");
                ((GDataRequestFactory)service.RequestFactory).KeepAlive = false;
                service.setUserCredentials(username, password);
                //force the service to authenticate
                DocumentsListQuery query = new DocumentsListQuery();
                query.NumberToRetrieve = 1;
                service.Query(query);
                loggedIn = true;
            }
            catch (AuthenticationException e)
            {
                loggedIn = false;
                service = null;
                throw e;
            }
        }

        public void Logout()
        {
            loggedIn = false;
            service = null;
        }

        public void UploadFile(string fileName)
        {
            if (Settings.Default.IsUpdToGoogle == false)
                return;

            if (!loggedIn)
            {
                throw new ApplicationException("Need to be logged in to upload documents.");
            }
            else
            {
                DocumentsListQuery query = new DocumentsListQuery();
                DocumentsFeed feed = service.Query(query);
                foreach (DocumentEntry entry in feed.Entries)
                {
                    if (fileName.Contains(entry.Title.Text.Trim()))
                    {
                        entry.Delete();
                        break;
                    }
                }

                lastUploadEntry = service.UploadDocument(fileName, null);
                
            }
        }

        public void UpdateDataToGoogleDoc(string FilePath)
        {
            try
            {
                if (Settings.Default.GAC.Length < 2)
                    return;

                Login(Settings.Default.GAC, Settings.Default.GPW);
                UploadFile(FilePath);
                Logout();
            }
            catch (Exception ex)
            {
                Console.WriteLine(FilePath+" 上傳檔案至google失敗!!" );
                Console.WriteLine("*************************************");
                Console.WriteLine(ex.Message);
                Console.WriteLine("*************************************");
            }
        }

    }
}
